package com.example.demo.service;

import com.example.demo.entity.LogEntity;
import com.example.demo.mapper.LogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-06-05
 * Time: 20:28
 */
@Service
public class LogService {
    @Autowired
    private LogMapper logMapper;

    @Transactional(propagation = Propagation.NESTED)
    public int add(LogEntity log) {
        int result = 0;
        try {
            result = logMapper.add(log);
            System.out.println("添加日志条数 : " + result);
            int a = 10 / 0;
        } catch (Exception e) {
            // 主动对异常进行处理回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }
}
